package com.xiaoxing.salesclient.mvp.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.SpinnerStyle;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.xiaoxing.gifloadingview.LoadingDialogUtil;
import com.xiaoxing.salesclient.di.component.DaggerAppointTimeComponent;
import com.xiaoxing.salesclient.di.module.AppointTimeModule;
import com.xiaoxing.salesclient.mvp.contract.AppointTimeContract;
import com.xiaoxing.salesclient.mvp.model.entity.AppointDoctor;
import com.xiaoxing.salesclient.mvp.model.entity.AppointSet;
import com.xiaoxing.salesclient.mvp.model.entity.AppointTime;
import com.xiaoxing.salesclient.mvp.model.entity.ProcessList;
import com.xiaoxing.salesclient.mvp.presenter.AppointTimePresenter;
import com.xiaoxing.salesclient.mvp.ui.adapter.AppointTimeAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import me.jessyan.armscomponent.commonres.utils.ToolbarUtils;
import me.jessyan.armscomponent.commonsdk.core.BaseConstants;
import me.jessyan.armscomponent.commonsdk.core.RouterHub;
import xiaoxing.com.salesclient.R;
import xiaoxing.com.salesclient.R2;

import static android.support.v7.widget.DividerItemDecoration.VERTICAL;
import static com.jess.arms.utils.Preconditions.checkNotNull;


@Route(path = RouterHub.SALES_CLIENT_APPOINT_TIME_ACTIVITY)
public class AppointTimeActivity extends BaseActivity<AppointTimePresenter> implements AppointTimeContract.View, OnRefreshListener {


    private static boolean mIsNeedDemo = true;
    @BindView(R2.id.empty_text)
    TextView empty_text;
    @BindView(R2.id.empty_image)
    ImageView empty_image;

    @BindView(R2.id.empty)
    View mEmptyLayout;
    @BindView(R2.id.recyclerView)
    RecyclerView mRecyclerView;
    @BindView(R2.id.refreshLayout)
    RefreshLayout mRefreshLayout;
    @BindView(R2.id.img_head)
    ImageView imgHead;
    @BindView(R2.id.tv_name)
    TextView tvName;
    @BindView(R2.id.tv_time)
    TextView tvTime;
    private AppointTimeAdapter mAdapter;
    private List<AppointTime.DataBean.ListBean> mDataBeanList = new ArrayList<>();
    private String mDoctorId;
    private String mKeHuUid;

    @Autowired
    String date;

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        DaggerAppointTimeComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .appointTimeModule(new AppointTimeModule(this))
                .build()
                .inject(this);

        ARouter.getInstance().inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_appoint_time; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {

        ToolbarUtils.initToolbarTitleBack(this, "預約時間");

        initRefreshLayout();
        initRecyclerView();
        initEmpty();

    }

    private void initRefreshLayout() {
        mRefreshLayout.setRefreshHeader(new ClassicsHeader(this).setSpinnerStyle(SpinnerStyle.FixedBehind).setPrimaryColorId(R.color.public_colorPrimary).setAccentColorId(android.R.color.white));
        mRefreshLayout.setOnRefreshListener(this);
        mRefreshLayout.autoRefresh();
        mRefreshLayout.setEnableLoadMore(false);
    }

    private void initRecyclerView() {
        mRecyclerView.setLayoutManager(new LinearLayoutManager(AppointTimeActivity.this));
        mRecyclerView.addItemDecoration(new DividerItemDecoration(AppointTimeActivity.this, VERTICAL));
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        mRecyclerView.setAdapter(mAdapter = new AppointTimeAdapter(AppointTimeActivity.this, mDataBeanList));

        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

                if (mDataBeanList.get(position).getStatus() == 1) {

                    ArmsUtils.snackbarText("此時間段已被預約");
                    return;
                }

                if (mDoctorId != null && mKeHuUid != null) {
                    mPresenter.appointSet(date, mDataBeanList.get(position).getTime_id(), mSharedPreferencesHelper.getString(BaseConstants.TOKEN), mDoctorId, mKeHuUid);
                }
            }
        });
    }

    private void initEmpty() {
        empty_image.setImageResource(R.drawable.ic_empty);
        empty_text.setTextColor(getResources().getColor(R.color.public_white));
        empty_text.setText("暫無數據下拉刷新");
    }


    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {


        AppointDoctor.DataBean dataBean = (AppointDoctor.DataBean) mSharedPreferencesHelper.getBean("appointDoctor_DataBean");

        mDoctorId = dataBean.getDoctor_id();
        mKeHuUid = mSharedPreferencesHelper.getString("ke_hu_uid");
        Glide.with(this).load(dataBean.getCover()).into(imgHead);
        tvName.setText(dataBean.getDoctor_name());
        tvTime.setText(date);

        getCheckDetail();
        mRefreshLayout.finishRefresh();
        mEmptyLayout.setVisibility(View.GONE);

    }

    private void getCheckDetail() {
        mPresenter.checkDetail(mSharedPreferencesHelper.getString(BaseConstants.TOKEN), mDoctorId, date, mSharedPreferencesHelper.getString(BaseConstants.SHOP_ID));
    }

    @Override
    public void showLoading() {
        LoadingDialogUtil.showGifdialog2(getSupportFragmentManager(), R.drawable.public_loading);
    }

    @Override
    public void hideLoading() {
        LoadingDialogUtil.dismissDialog();
    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.snackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }

    @Override
    public void checkDetailSuccess(AppointTime appointTime) {

        if (appointTime != null && appointTime.getData() != null && appointTime.getData() != null && appointTime.getData().getList().size() > 0) {

            mDataBeanList.clear();
            mDataBeanList.addAll(appointTime.getData().getList());
            mAdapter.notifyDataSetChanged();

        }

    }

    @Override
    public void appointSetSuccess(AppointSet appointSet) {

        if (appointSet != null && appointSet.getCode().equals("200")) {
//            getCheckDetail();
//            ArmsUtils.makeText(this, appointSet.getMsg());
            ArmsUtils.makeText(this, "預約成功");
            killMyself();
        } else {
            ArmsUtils.makeText(this, appointSet.getMsg());
        }
    }

}
